package jok.io;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintWriterDemo {

	public static void main(String[] args) {

		printFile();

		printToOut();

		FileDemo.deleteFile("D:/a.txt");
	}

	public static void printFile() {
		PrintWriter pw = null;
		try {
			pw = new PrintWriter("D:/a.txt");
			pw.print(true);
			pw.println(false);
			pw.print('c');
			pw.println('d');
			pw.print(123);
			pw.println(456);
			pw.print("hello");
			pw.println("world");
			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (pw != null)
				pw.close();
		}
	}

	/**
	 * 从文件读取数据打印至控制台（字符流 至 字节流）
	 */
	public static void printToOut() {
		BufferedReader br = null;
		PrintWriter pw = null;
		try {
			br = new BufferedReader(new FileReader("D:/a.txt"));
			pw = new PrintWriter(System.out);

			String line = null;
			while ((line = br.readLine()) != null) {
				pw.println(line);
				pw.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pw != null)
					pw.close();
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
